package jwdt.xadmin.bbs.action;

import jwdt.common.action.BaseAction;
import jwdt.xadmin.bbs.bean.TBuser;
import jwdt.xadmin.bbs.service.UserManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;
import com.ezerg.web.Page;
import com.ezerg.web.annotation.Controller;
import com.ezerg.web.annotation.RequestMapping;

@Controller("/xadmin/bbs")
public class UserManageAction extends BaseAction {
	
	private UserManageService userManageService = (UserManageService)app.getBean("bbsuserManageService");
	
	@RequestMapping("/listUser")
	public Page doListUser(Form f, Module m) throws Exception {
		PageUtil pu = (PageUtil)f.copy2Class(PageUtil.class);
		TBuser obj = (TBuser)f.copy2Class(TBuser.class);
    	return m.getWritePage(f, userManageService.doListUser(pu, obj).toJson()); 
	}
	
	@RequestMapping("/saveUser")
	public Page doSaveUser(Form f, Module m) throws Exception {
		int userid = f.getInt("userid");
		TBuser obj = (TBuser)f.copy2Class(TBuser.class);
		if(userid == 0) {
			return m.getWritePage(f, userManageService.doAddact(obj));
		} else {
			return m.getWritePage(f, userManageService.doEditact(obj, userid));
		}
	}
	
	@RequestMapping("/delUser")
	public Page doDelUser(Form f, Module m) throws Exception {
		int userid = f.getInt("userid");
		return m.getWritePage(f, userManageService.doDelact(userid));
	}
	
	@RequestMapping("/updateJinyong")
	public Page doUpdateJinyong(Form f, Module m) throws Exception {
		int userid = f.getInt("userid");
		int result = f.getInt("result");
		return m.getWritePage(f, userManageService.doUpdateState(userid,"lockuser="+result));
	}
	
	@RequestMapping("/updateShenhe")
	public Page doUpdateShenhe(Form f, Module m) throws Exception {
		int userid = f.getInt("userid");
		int result = f.getInt("result");
		return m.getWritePage(f, userManageService.doUpdateState(userid,"bbstype="+result));
	}
	
	@RequestMapping("/userWindow")
	public Page doLoad(Form f, Module m) throws Exception {
		int userid = f.getInt("userid");
		f.add("obj", userManageService.doLoad(userid));
		return m.findPage("/xadmin/bbs/userWindow");
	}
	
	@RequestMapping("/userEditInfo")
	public Page doEditUserInfo(Form f, Module m) throws Exception {
		TBuser user = (TBuser) f.getSession("userInfo");
		f.add("obj", userManageService.doLoad(user.getUserid()));
		return m.findPage("/xadmin/bbs/userEditInfo");
	}
	@RequestMapping("/listUserForCombox")
	public Page doListUserForCombox(Form f, Module m) throws Exception {
		TBuser userInfo = (TBuser) f.getSession("userInfo");
		String where = "";
		String flag = f.getString("flag");
		if(!StringUtil.nullOrBlank(flag)){
			where =" where userid != "+userInfo.getUserid();
		}
    	return m.getJsonPage(f, Jdb.get().query("select userid, username from t_user "+where+" ORDER BY userid asc"));
	}
}
